package com.ubxtech.core.utils;

import cn.hutool.core.util.StrUtil;
import com.ubxtech.core.exception.ErrorCodeCore;
import com.ubxtech.core.exception.RRException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Victor.Xiao
 * @since 2021-12-15 19:35
 **/
@Slf4j
public class SignCheckUtil {

    public static int FIVE_MINUTE = 600;

    public static boolean checkSign(String deviceId, String appKey, String sign, int ignoreTimeFlag, boolean throwEx) {
        String[] signs = sign.split(" ");
        if (signs.length == 4) {
            try {
                Long requestTime = Long.parseLong(signs[1]);
                Long now = System.currentTimeMillis() / 1000L;
                String randomStr = signs[2];
                long secondRequestTime = requestTime;
                if (Long.toString(secondRequestTime).length() == 13) {
                    secondRequestTime /= 1000L;
                }
                //校验时间
                if (ignoreTimeFlag != 1 && Math.abs(now - secondRequestTime) > FIVE_MINUTE) {
                    log.info("校验算法时间失败,{},{},{}", deviceId, appKey, sign);
                    if (throwEx) {
                        throw new RRException(ErrorCodeCore.SIGN_TIME_ERROR);
                    }
                    return false;
                }
//                if (Math.abs(now - secondRequestTime) <= FIVE_MINUTE) {
                StringBuilder builder = new StringBuilder();
                if (StrUtil.isEmpty(deviceId)) {
                    builder.append(requestTime).append(appKey).append(randomStr);
                } else {
                    builder.append(requestTime).append(appKey).append(randomStr).append(deviceId);
                }

                String md5 = Md5Util.md5(builder.toString());

                builder = new StringBuilder(md5).append(" ").append(requestTime).append(" ").append(randomStr).append(" ").append("v2");
                if (sign.equalsIgnoreCase(builder.toString())) {
                    return true;
                }
                log.info("校验算法md5不等,{},{},{},{}", deviceId, appKey, sign, builder.toString());
                if (throwEx) {
                    throw new RRException(ErrorCodeCore.SIGN_MD5_ERROR);
                }
//                }else{
//                    log.info("校验算法时间失败,{},{},{}",deviceId,appKey,sign);
//                }
            } catch (Exception e) {
                e.printStackTrace();
                log.info("校验算法出错", e);

            }
        } else {
            log.info("校验算法失败,格式不对,{},{},{}", deviceId, appKey, sign);
            if (throwEx) {
                throw new RRException(ErrorCodeCore.SIGN_CONTENT_ERROR);
            }
        }
        if (throwEx) {
            throw new RRException(ErrorCodeCore.SIGN_ERROR);
        }
        return false;
    }


    public static boolean checkSign(String deviceId, String appKey, String sign, int ignoreTimeFlag) {
        return SignCheckUtil.checkSign(deviceId, appKey, sign, ignoreTimeFlag, false);
    }

    public static void main(String[] args) {
        System.out.println(SignUtil.getSignV2("d0c79855ad724808b09c8d58a7f14300", "systemFile"));
        boolean b = SignCheckUtil.checkSign("systemFile", "300000011", "8449cca1d12f537ac014c205929fef23 1678186275 1234567890 v2",0);
        System.out.println(b);
    }
}
